package com.bwt.top.bwt_ad_flutter.ad;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.bwt.top.ad.bean.NativeAdInfo;
import com.bwt.top.ad.impl.NativeAd;
import com.bwt.top.ad.listener.NativeAdListener;
import com.bwt.top.bwt_ad_flutter.AdErrorUtil;
import com.bwt.top.bwt_ad_flutter.config.BwtAdEventConfig;
import com.bwt.top.bwt_ad_flutter.manager.BwtAdManager;
import com.bwt.top.bwt_ad_flutter.model.AdParams;
import com.bwt.top.bwt_ad_flutter.util.LogUtil;
import com.bwt.top.exception.AdError;

import java.util.List;

public class BwtNativeExpressAd extends NativeAdListener {
    @NonNull
    private final AdParams adParams;
    private NativeAd nativeAd;
    private NativeAdInfo nativeAdInfo;

    public BwtNativeExpressAd(@NonNull AdParams adParams, @NonNull Activity activity) {
        this.adParams = adParams;
        this.nativeAd = new NativeAd(activity);
    }

    @Override
    public void onAdReceive(List<NativeAdInfo> list) {
        if (list != null && list.size() > 0) {
            nativeAdInfo = list.get(0);
            LogUtil.log("广告获取成功，adId: " + adParams.getAdId()+nativeAdInfo);
            BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_LOAD, adParams);
        } else {
            LogUtil.log("广告获取失败，adId: " + adParams.getAdId() + ", error: 获取广告结果为空");
            onAdFailed(new AdError(-1, "获取广告结果为空"));
        }
    }

    @Override
    public void onAdFailed(AdError adError) {
        LogUtil.log("广告获取失败，adId: " + adParams.getAdId() + ", error: " + AdErrorUtil.getError(adError));
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_FAILED, adParams, AdErrorUtil.getError(adError));
        release();
    }

    @Override
    public void onAdClick(NativeAdInfo nativeAdInfo) {
        LogUtil.log("广告被点击，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_CLICK, adParams);
    }

    @Override
    public void onAdExpose(NativeAdInfo nativeAdInfo) {
        LogUtil.log("广告被展示，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_EXPOSE, adParams);
    }

    @Override
    public void onAdClose(NativeAdInfo nativeAdInfo) {
        LogUtil.log("广告被关闭，adId: " + adParams.getAdId());
        BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_CLOSE, adParams);
        release();
    }

    @Override
    public void onAdClick(NativeAdInfo nativeAdInfo, String s) {
    }

    @Override
    public void onRenderFailed(NativeAdInfo nativeAdInfo, AdError adError) {
        LogUtil.log("广告渲染失败，adId: " + adParams.getAdId() + ", error: " + AdErrorUtil.getError(adError));
    }

    /**
     * 加载模板信息流广告
     */
    public void loadAd() {
        // 其他相关的配置请参考文档
        nativeAd.setAdId(adParams.getPosId());
        nativeAd.setAdListener(this);

        LogUtil.log("广告加载中，adId: " + adParams.getAdId());
        nativeAd.loadAd();
    }

    @NonNull
    public AdParams getAdParams() {
        return adParams;
    }

    public NativeAd getNativeAd() {
        return nativeAd;
    }

    public NativeAdInfo getNativeAdInfo() {
        return nativeAdInfo;
    }

    /**
     * 释放广告
     */
    public void release() {
        try {
            if (nativeAdInfo != null) {
                nativeAdInfo.release();
                nativeAdInfo = null;
            }

            if (nativeAd != null) {
                nativeAd.release();
                nativeAd = null;
            }
            LogUtil.log("广告被释放, adId: " + adParams.getAdId());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
